/*
 * Copyright 2009 Tran Nhut Trung.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package com.gdevelop.gwtxp.rebind;

import java.io.File;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class Utils {
  private static List<File> BASE_DIRS = new ArrayList<File>();
  static{
    String classpath = System.getProperty("java.class.path");
    StringTokenizer st = new StringTokenizer(classpath, File.pathSeparator);
    while (st.hasMoreTokens()){
      String path = st.nextToken();
      File f = new File(path);
      if (f.isDirectory()){
        BASE_DIRS.add(f);
      }
    }
  }
  
  // TODO: remove later?
  public static String[] _getPackageAndClassName(File file){
    String path = file.getPath();
    String packageName;
    String simpleName;
    
    // remove src/
    int idx = path.indexOf(File.separator);
    if (idx >= 0){
      packageName = path.substring(idx+1);
    }else{
      packageName = path;
    }
    packageName = packageName.replace(File.separatorChar, '.');
    idx = packageName.lastIndexOf('.');
    // remove extension
    if (idx >= 0){
      packageName = packageName.substring(0, idx);
    }
    // split packageName & simpleName
    idx = packageName.lastIndexOf('.');
    if (idx >= 0){
      simpleName = packageName.substring(idx+1);
      packageName = packageName.substring(0, idx);
    }else{
      simpleName = packageName;
      packageName = "";
    }
    
    return new String[]{packageName, simpleName};
  }
  
  public static String[] getPackageAndClassName(File file){
    String packageName = file.getParentFile().getPath();
    for (File dir : BASE_DIRS){
      if (packageName.startsWith(dir.getPath())){
        packageName = packageName.substring(dir.getPath().length());
        break;
      }
    }
    
    String simpleName = file.getName();
    String ext;
    
    // remove src/
    int idx = packageName.indexOf(File.separator);
    if (idx >= 0){
      packageName = packageName.substring(idx+1);
    }
    packageName = packageName.replace(File.separatorChar, '.');

    idx = simpleName.indexOf('.');
    // remove extension
    if (idx >= 0){
      ext = simpleName.substring(idx+1);
      simpleName = simpleName.substring(0, idx);
    }else{
      ext = "";
    }

    return new String[]{packageName, simpleName, ext};
  }
  
  public static void main(String[] args) {
    String[] x = getPackageAndClassName(new File("src\\com\\google\\gwt\\sample\\showcase\\client\\content\\lists\\CwListBoxPage.gwtxp.xml"));
    System.out.println(x[0]);
    System.out.println(x[1]);
    System.out.println(x[2]);

    x = getPackageAndClassName(new File("src\\com\\google\\gwt\\sample\\showcase\\client\\content\\lists\\CwListBoxPage.gwtxp"));
    System.out.println(x[0]);
    System.out.println(x[1]);
    System.out.println(x[2]);
  }
}
